import { getSystemAddress } from "@/api/user";
export default {
  data() {
    return {};
  },
  created() {
    // 判断是否微信浏览器打开
    var ua = navigator.userAgent.toLowerCase();
    var isWeixin = ua.indexOf("micromessenger") !== -1;
    if (isWeixin) {
      const code = localStorage.getItem("loginCode");
      if (code) {
        this.qywxLoginByCode(code);
      } else {
        this.getCode();
      }
    }
  },
  methods: {
    // 通过code自动登录
    qywxLoginByCode(code) {
      const _stateArr = JSON.parse(localStorage.getItem("_stateArr"));
      let _qyhCorpId = _stateArr.corpId;
      let _qyhAgentId = _stateArr.agentId;
      localStorage.setItem("_qyhCorpId", _qyhCorpId);
      localStorage.setItem("_qyhAgentId", _qyhAgentId);
      this.loading = true;
      this.$store.commit("user/SET_LOGIN_LOADING", true);
      this.qyhCorpId = _qyhCorpId;
      const paramCode = {
        code: code,
        qyhCorpId: _qyhCorpId,
        qyhAgentId: _qyhAgentId,
      };
      this.$store
        .dispatch("user/loginByWeCom", paramCode)
        .then((res) => {
          localStorage.removeItem("loginCode");
          localStorage.removeItem("_stateArr");
          this.loading = false;
          console.log(isEmpty(localStorage.getItem("msg_id")), "++++++++3");
          if (localStorage.getItem("msg_id") == "undefined") {
            localStorage.removeItem("msg_id");
          }
          if (
            localStorage.getItem("msg_id") &&
            !isEmpty(localStorage.getItem("msg_id")) &&
            localStorage.getItem("msg_id") != "undefined"
          ) {
            getMessageInfo(localStorage.getItem("msg_id")).then((res) => {
              localStorage.removeItem("msg_id");
              this.jumpMsgUrl(res);
            });
          } else {
            this.$router.push({
              path: this.redirect || "/",
              query: this.otherQuery,
            });
          }
        })
        .catch(() => {
          this.loading = false;
          this.$store.commit("user/SET_LOGIN_LOADING", false);
        });
    },

    // 获取自动登录的code
    getCode() {
      // 获取路径参数
      const query = this.$route.query;
      let _qyhCorpId = "";
      let _qyhAgentId = "";
      if (query.qyhCorpId) {
        _qyhCorpId = query.qyhCorpId;
      } else if (localStorage.getItem("_qyhCorpId")) {
        _qyhCorpId = localStorage.getItem("_qyhCorpId");
      }
      if (query.qyhAgentId) {
        _qyhAgentId = query.qyhAgentId;
      } else if (localStorage.getItem("_qyhAgentId")) {
        _qyhAgentId = localStorage.getItem("_qyhAgentId");
      }
      let redirect_uri = "";
      this.qyhCorpId = _qyhCorpId;
      console.log(_qyhCorpId, _qyhAgentId, "_qyhCorpId && _qyhAgentId");
      if (_qyhCorpId && _qyhAgentId) {
        let state = _qyhCorpId + "_" + _qyhAgentId;
        getSystemAddress("pc").then((res) => {
          console.log(res, "redirect_uri");
          redirect_uri = encodeURIComponent(res.msg);
          const url = "https://open.weixin.qq.com/connect/oauth2/authorize";
          const wx_url = `${url}?appid=${_qyhCorpId}&redirect_uri=${redirect_uri}&response_type=code&scope=snsapi_privateinfo&state=${state}&agentid=${_qyhAgentId}#wechat_redirect`;
          window.location.replace(wx_url);
        });
      } else {
        this.$message.warning("缺少自动登录必要参数，请手动或扫码登录。");
      }
    },
  },
};
